using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using star_questionnair.Services;
using star_questionnair.Services.impl;
using star_questionnair.Utilities;
using star_questionnairquestionnair.Services;

var builder = WebApplication.CreateBuilder(args);

//注册服务
// 注册 IMemoryCache
builder.Services.AddMemoryCache();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IQuestionService, QuestionService>();
builder.Services.AddScoped<IAnswerService, AnswerService>();
builder.Services.AddScoped<IQuestionnaireService, QuestionnaireService>();
builder.Services.AddScoped<IOptionService, OptionService>();
builder.Services.AddScoped<IHistoryService, HistoryService>();   // 确保 HistoryService 已正确注册
builder.Services.AddScoped<IWordCloudService, WordCloudService>();
builder.Services.AddScoped<ITimeService, TimeService>();
builder.Services.AddScoped<IResponseService, ResponseService>();
builder.Services.AddScoped<IQuestionnaireService, QuestionnaireService>();
builder.Services.AddScoped<ICorrelationAnalysisService, CorrelationAnalysisService>();
builder.Services.AddTransient<IQuestionnaireService,QuestionnaireService>();
builder.Services.AddTransient<ChoiceAnalysisService>();
builder.Services.AddTransient<ISortQuestionAnalysisService, SortQuestionAnalysisService>();
builder.Services.AddTransient<ILevelQuestionAnalysisService, LevelQuestionAnalysisService>();
builder.Services.AddTransient<IDifferenceChoiceQuestionAnalysisService, DifferenceChoiceQuestionAnalysisService>();
builder.Services.AddTransient<IDifferenceLevelQuestionAnalysisService, DifferenceLevelQuestionAnalysisService>();
builder.Services.AddTransient<IDifferenceSortQuestionAnalysisService, DifferenceSortQuestionAnalysisService>();
builder.Services.AddTransient<EmailService>();
builder.Services.AddTransient<CrossAnalysisService>();
// 使用局部函数 ConfigureDbContext
void ConfigureDbContext(DbContextOptionsBuilder options) =>
    options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"),
        new MySqlServerVersion(new Version(8, 0, 21))); // 使用 MySQL 8.0
// 配置服务
builder.Services.AddDbContext<ApplicationDbContext>(ConfigureDbContext);
builder.Services.AddControllers();

// 配置 CORS 策略
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowVueApp", policy =>
    {
        policy.WithOrigins("http://localhost:8085")  // 允许 Vue 前端的地址
            .AllowAnyMethod()                   // 允许任何 HTTP 方法
            .AllowAnyHeader()                   // 允许任何请求头
            .AllowCredentials();                // 允许携带 Cookies（如果需要）
    });
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

// 启用 CORS 中间件
app.UseCors("AllowVueApp");  

app.UseAuthorization();

app.MapControllers();

app.Run();
